package com.zw.yzk.kabu.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.GoodsListAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.GoodsModel;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/2/9.
 */

public class HotGoodsActivity extends BaseActivity {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.goods_list)
    RecyclerView goodsList;

    private GoodsListAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_hot_goods;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.hot_goods))
                .build();
    }

    @Override
    public void initComponent() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initRecyclerView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        goodsList.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
        adapter = new GoodsListAdapter(null);
        goodsList.setAdapter(adapter);
        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                adapter.refresh(createGoods());
                                refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
                            }
                        }, 2000);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                adapter.add(createGoods());
                                refreshLayout.loadMoreFinish(State.LOADING_SUCCEED);
                            }
                        }, 2000);
                    }
                }).build();
    }

    private List<GoodsModel> createGoods() {
        List<GoodsModel> goodList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            GoodsModel model = new GoodsModel();
            model.setName("商品" + i);
            model.setPrice(388.00f + i);
            model.setShopName("古川");
            switch (i) {
                case 0:
                    model.setImage("http://b.hiphotos.baidu.com/image/pic/item/a686c9177f3e670900d880193fc79f3df9dc5578.jpg");
                    break;
                case 1:
                    model.setImage("http://e.hiphotos.baidu.com/image/pic/item/6c224f4a20a44623d5c4cd689a22720e0cf3d7aa.jpg");
                    break;
                case 2:
                    model.setImage("http://d.hiphotos.baidu.com/image/pic/item/b64543a98226cffca5ddc9b3bb014a90f603ea38.jpg");
                    break;
                case 3:
                    model.setImage("http://a.hiphotos.baidu.com/image/pic/item/08f790529822720eac324afa79cb0a46f21fab36.jpg");
                    break;
                case 4:
                    model.setImage("http://b.hiphotos.baidu.com/image/pic/item/023b5bb5c9ea15ce27989008b4003af33a87b2b8.jpg");
                    break;
                case 5:
                    model.setImage("http://e.hiphotos.baidu.com/image/pic/item/4bed2e738bd4b31c4b51e5ba85d6277f9e2ff846.jpg");
                    break;
                case 6:
                    model.setImage("http://c.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fb2064e2266f224f4a20a4dd14.jpg");
                    break;
            }
            goodList.add(model);
        }
        return goodList;
    }
}
